package com.design.chain;

/**
 * @ClassName Handler.java
 * @Description 抽象处理者
 * @author Jon
 * @date 2018年6月18日 下午3:00:30
 */
public abstract class Handler {
	private Handler nextHandler;
	/**   
	 * Description  	处理者对请求做处理	
	 */ 
	public final Response handleMessage(Request request) {
		Response response = null;
		//判断是否是自己的处理级别
		if (this.getHandlerLevel().equals(request.getRequestLevel())) {
			response = this.echo(request);
		} else {
			if (this.nextHandler != null) {
				response = this.nextHandler.handleMessage(request);
			} else {
				System.out.println("未找到合适的处理者!");
			}
		}
		return response;
	}
	
	public void setNext(Handler handler) {
		this.nextHandler = handler;
	}
	/**   
	 * Description  	获取当前处理者的处理级别	
	 */ 
	protected abstract Level getHandlerLevel();
	/**   
	 * Description  	具体的处理任务	
	 */ 
	protected abstract Response echo(Request request);
}
